<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
			<el-form-item label="设备名称" prop="equipmentName">
				<el-input v-model="queryParams.equipmentName" placeholder="请输入设备名称" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="创建时间" prop="createDate">
				<el-date-picker clearable v-model="queryParams.createDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择创建时间">
				</el-date-picker>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
				<el-button icon="Refresh" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['base:equipment:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['base:equipment:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['base:equipment:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['base:equipment:export']">导出</el-button>
			</el-col>
			<right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="equipmentList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="设备名称" align="center" prop="equipmentName" />
			<el-table-column label="设备IP" align="center" prop="ip" />
			<el-table-column label="设备端口" align="center" prop="port" />
			<el-table-column label="描述" align="center" prop="describ" />
			<el-table-column label="状态" align="center" prop="status">
				<template #default="scope">
					<dict-tag :options="status" :value="scope.row.status" />
				</template>
			</el-table-column>
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template #default="scope">
					<el-button type="text" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['base:equipment:edit']">修改</el-button>
					<el-button type="text" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['base:equipment:remove']">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
		 @pagination="getList" />

		<!-- 添加或修改设备管理对话框 -->
		<el-dialog :title="title" v-model="open" width="500px" append-to-body>
			<el-form ref="equipmentRef" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="设备名称" prop="equipmentName">
					<el-input v-model="form.equipmentName" placeholder="请输入设备名称" />
				</el-form-item>
				<el-form-item label="设备IP" prop="ip">
					<el-input v-model="form.ip" placeholder="请输入IP" />
				</el-form-item>
				<el-form-item label="设备端口" prop="port">
					<el-input v-model="form.port" placeholder="请输入端口" />
				</el-form-item>
				<el-form-item label="描述" prop="describ">
					<el-input v-model="form.describ" placeholder="请输入描述" />
				</el-form-item>
				<el-form-item label="状态" prop="status">
					<el-select v-model="form.status" placeholder="请选择状态">
						<el-option v-for="dict in status" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<template #footer>
				<div class="dialog-footer">
					<el-button type="primary" @click="submitForm">确 定</el-button>
					<el-button @click="cancel">取 消</el-button>
				</div>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="Equipment">
	import {
		listEquipment,
		getEquipment,
		delEquipment,
		addEquipment,
		updateEquipment
	} from "@/api/base/equipment";
	const {
		proxy
	} = getCurrentInstance();
	const {
		status,
		is_delflag
	} = proxy.useDict('status', 'is_delflag');

	const equipmentList = ref([]);
	const open = ref(false);
	const loading = ref(true);
	const showSearch = ref(true);
	const ids = ref([]);
	const single = ref(true);
	const multiple = ref(true);
	const total = ref(0);
	const title = ref("");

	const data = reactive({
		form: {},
		queryParams: {
			pageNum: 1,
			pageSize: 10,
			equipmentName: null,
			equipmentCode: null,
			storehouseId: null,
			createDate: null,
		},
		rules: {
			equipmentName: [{
				required: true,
				message: "设备名称不能为空",
				trigger: "blur"
			}],
			equipmentCode: [{
				required: true,
				message: "设备编号不能为空",
				trigger: "blur"
			}],
			ip: [{
				required: true,
				message: "IP不能为空",
				trigger: "blur"
			}],
			port: [{
				required: true,
				message: "端口不能为空",
				trigger: "blur"
			}],
		}
	});

	const {
		queryParams,
		form,
		rules
	} = toRefs(data);

	/** 查询设备管理列表 */
	function getList() {
		loading.value = true;
		listEquipment(queryParams.value).then(response => {
			equipmentList.value = response.rows;
			total.value = response.total;
			loading.value = false;
		});
	}

	// 取消按钮
	function cancel() {
		open.value = false;
		reset();
	}

	// 表单重置
	function reset() {
		form.value = {
			equipmentId: null,
			equipmentName: null,
			equipmentCode: null,
			storehouseId: null,
			ip: null,
			port: null,
			describ: null,
			createDate: null,
			updateDate: null,
			status: null,
			delflag: null
		};
		proxy.resetForm("equipmentRef");
	}
	/** 搜索按钮操作 */
	function handleQuery() {
		queryParams.value.pageNum = 1;
		getList();
	}

	/** 重置按钮操作 */
	function resetQuery() {
		proxy.resetForm("queryRef");
		handleQuery();
	}

	// 多选框选中数据
	function handleSelectionChange(selection) {
		ids.value = selection.map(item => item.equipmentId);
		single.value = selection.length != 1;
		multiple.value = !selection.length;
	}

	/** 新增按钮操作 */
	function handleAdd() {
		reset();
		open.value = true;
		title.value = "添加设备管理";
	}

	/** 修改按钮操作 */
	function handleUpdate(row) {
		reset();
		const equipmentId = row.equipmentId || ids.value
		getEquipment(equipmentId).then(response => {
			form.value = response.data;
			open.value = true;
			title.value = "修改设备管理";
		});
	}

	/** 提交按钮 */
	function submitForm() {
		proxy.$refs["equipmentRef"].validate(valid => {
			if (valid) {
				if (form.value.equipmentId != null) {
					updateEquipment(form.value).then(response => {
						proxy.$modal.msgSuccess("修改成功");
						open.value = false;
						getList();
					});
				} else {
					addEquipment(form.value).then(response => {
						proxy.$modal.msgSuccess("新增成功");
						open.value = false;
						getList();
					});
				}
			}
		});
	}

	/** 删除按钮操作 */
	function handleDelete(row) {
		const equipmentIds = row.equipmentId || ids.value;
		proxy.$modal.confirm('是否确认删除设备管理编号为"' + equipmentIds + '"的数据项？').then(function() {
			return delEquipment(equipmentIds);
		}).then(() => {
			getList();
			proxy.$modal.msgSuccess("删除成功");
		}).catch(() => {});
	}

	/** 导出按钮操作 */
	function handleExport() {
		proxy.download('base/equipment/export', {
			...queryParams.value
		}, `equipment_${new Date().getTime()}.xlsx`)
	}

	getList();
</script>
